<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="110px">

      <el-form-item label="药剂名称" prop="prescriptionName">
        <el-input v-model="queryParams.prescriptionName" placeholder="请输入药剂名称" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="药剂类型" prop="prescriptionTypeKey">
        <!-- <el-input v-model="queryParams.prescriptionTypeKey" placeholder="请输入药剂类型" clearable size="small"
          @keyup.enter.native="handleQuery" /> -->

        <el-select v-model="queryParams.prescriptionTypeKey" clearable size="small">
          <el-option v-for="item in prescriptiontypeList" :key="item.prescriptionkey" :value="item.prescriptionkey"
            :label="item.prescriptionname" />

        </el-select>
      </el-form-item>

      <el-form-item label="功效与作用" prop="function">
        <el-input v-model="queryParams.function" placeholder="请输入功能" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['zhongyao:prescription:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['zhongyao:prescription:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['zhongyao:prescription:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['zhongyao:prescription:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="prescriptionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="药剂名称" align="center" prop="prescriptionName" width="80" />
      <el-table-column label="药剂类型" align="center" prop="prescriptionTypeName" width="80" />
      <el-table-column label="药剂组成" align="center" prop="prescriptionContent" />
      <el-table-column label="原文出处" align="center" prop="from" />

      <el-table-column label="图片" align="center" prop="picUrl">
          <template slot-scope="scope">
            <image-preview :src="scope.row.picUrl" :width="50" :height="50" />
          </template>
        </el-table-column>
      <el-table-column label="功效与作用" align="center" prop="function" />
      <!-- <el-table-column label="执法及用法" align="center" prop="picUrl" /> -->
      <el-table-column label="制法及用法" align="center">
        <template slot-scope="scope">
          <div class="contentClass">{{ delTag(scope.row.zhiFa) }}</div>
        </template>
      </el-table-column>


      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['zhongyao:prescription:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['zhongyao:prescription:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改方剂维护对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1100px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="药剂名称" prop="prescriptionName">
          <el-input v-model="form.prescriptionName" placeholder="请输入药剂名称" />
        </el-form-item>
        <el-form-item label="药剂类型">
          <el-select v-model="form.prescriptionTypeKey" clearable size="small">
            <el-option v-for="item in prescriptiontypeList" :key="item.prescriptionkey" :value="item.prescriptionkey"
              :label="item.prescriptionname" />
          </el-select>
          <!-- <el-input v-model="form.prescriptionTypeKey" placeholder="请输入药剂类型" /> -->
        </el-form-item>
        <el-form-item label="药剂组成">
          <el-input v-model="form.prescriptionContent" placeholder="请输入药剂组成" />

        </el-form-item>
        <el-form-item label="图片" prop="picUrl">
          <image-upload :limit="imgLimit" v-model="form.picUrl"/>
        </el-form-item>
        <el-form-item label="原文出处" prop="from">
          <el-input v-model="form.from" type="textarea" placeholder="请输入原文出处" />
        </el-form-item>
        <el-form-item label="功效与作用" prop="function">
          <el-input v-model="form.function" type="textarea" placeholder="请输入功效与作用" />
        </el-form-item>
        <el-form-item label="执法及用法" prop="zhiFa">
          <editor v-model="form.zhiFa" :min-height="142" />

        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPrescription, getPrescription, delPrescription, addPrescription, updatePrescription } from "@/api/zhongyao/prescription";

import { listPrescriptiontype } from "@/api/zhongyao/prescriptiontype";

export default {
  name: "Prescription",
  data() {
    return {
      imgLimit: 1 ,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      key: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 方剂维护表格数据
      prescriptionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        prescriptionKey: null,
        prescriptionId: null,
        prescriptionName: null,
        prescriptionContent: null,
        from: null,
        prescriptionTypeKey: null,
        function: null,
        picUrl: null,
        deleteFlag: null
      },
      prescriptiontypeList: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        prescriptionName: [
          { required: true, message: "名称不能为空", trigger: "blur" },
        ],
        prescriptionTypeKey: [
          { required: true, message: "类型不能为空", trigger: "blur" },
        ],
        prescriptionContent: [
          { required: true, message: "组成不能为空", trigger: "blur" },
        ],
        function: [
          { required: true, message: "功能不能为空", trigger: "blur" },
        ],

      }
    };
  },
  created() {
    this.getList();
    listPrescriptiontype().then(response => {
      this.prescriptiontypeList = response.rows;
      this.loading = false;
      console.log(response)
    });
  },
  methods: {
    //移除HTML标签
    delTag(str) {
      return str.replace(/<[^>]+>/g, "");
    },

    /** 查询方剂维护列表 */
    getList() {
      this.loading = true;
      listPrescription(this.queryParams).then(response => {
        this.prescriptionList = response.rows;
        this.total = response.total;
        this.loading = false;

      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        prescriptionKey: null,
        prescriptionId: null,
        prescriptionName: null,
        prescriptionContent: null,
        from: null,
        prescriptionTypeKey: null,
        function: null,
        picUrl: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        deleteFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.prescriptionKey)
      this.key = selection.map(item => item.prescriptionName)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加方剂维护";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const prescriptionKey = row.prescriptionKey || this.ids
      getPrescription(prescriptionKey).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改方剂维护";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.prescriptionKey != null) {
            updatePrescription(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPrescription(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const prescriptionKeys = row.prescriptionKey || this.ids;
      const id = row.prescriptionId || this.key;
      this.$modal.confirm('是否确认删除方剂维护名称为"' + id + '"的数据项？').then(function () {
        return delPrescription(prescriptionKeys);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('zhongyao/prescription/export', {
        ...this.queryParams
      }, `prescription_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
